package com.hanlink.dsp.ui.adapter;

import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import java.util.ArrayList;

/**
 * <pre>
 *
 *  BTDSP
 *
 *
 *  Copyright (C): 2015/10/18
 *
 * Description:
 *
 * Revision History
 * Date			  Author		Description
 * 2015/10/18        PeterVon       初始创建
 *
 * </pre>
 */
public class ViewPagerAdapter extends PagerAdapter {

    private int mPosition;
    private ArrayList<View> views;

    public void setViews(ArrayList<View> views) {
        this.views = views;
    }

    public void addViews(View view) {
        if (this.views == null) {
            views = new ArrayList<View>();
        }
        views.add(view);
        notifyDataSetChanged();
    }

    public ArrayList<View> getViewList() {
        return views;
    }

    @Override
    public int getCount() {
        return views == null ? 0 : views.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object o) {
        return view == o;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager) container).removeView(views.get(position));
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        mPosition = position;
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        layoutParams.gravity = Gravity.CENTER;
        ((ViewPager) container).addView(views.get(position), layoutParams);
        return views.get(position);
    }


    public int getPosition() {
        return mPosition;
    }

    @Override
    public void finishUpdate(ViewGroup container) {
        super.finishUpdate(container);
    }
}
